import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '@/views/LoginView.vue'
import Gwc from '@/views/GwcView.vue';
import Home from '@/views/HomeView.vue';
import Gun from '@/views/GunView.vue';
import GunDetail from '@/views/GunDetailView.vue'
import store from '@/store/index'
import Register from '@/views/RegisterView';



Vue.use(VueRouter)

const routes = [
  {
    path: '/admin',
    name: 'admin',
    component: () => import ('@/views/AdminView.vue') 
  },
  {
    path: '/',
    redirect: '/home'
  },
  {
    path: '/',
    name: 'index',
    component: () => import('@/views/index.vue'),
    children: [
      {
        path: '/home',
        name: 'Home',
        component: Home
      },
      {
        path: '/Login',
        name: 'Login',
        component: Login,
        meta: {
          isDL: store.state.isDL
        },
        beforeEnter: (to, from, next) => {
          // reject the navigation
          console.log(store.state.isDL);
          if (to.meta.isDL)
            next({ name: "Home" });
          else next()
        },
      },
      {
        path: '/Register',
        name: 'Register',
        component: Register,
        meta: {
          isDL: store.state.isDL
        },
        beforeEnter: (to, from, next) => {
          // reject the navigation
          console.log(store.state.isDL);
          if (to.meta.isDL)
            next({ name: "Home" });
          else next()
        },
      },
      {
        path: '/Gwc',
        name: 'Gwc',
        component: Gwc,
        meta: {
          isDL: store.state.isDL
        },
        beforeEnter: (to, from, next) => {
          // reject the navigation
          console.log(store.state.isDL);
          if (!to.meta.isDL)
            next({ name: "Login" });
          else next()
        },
      },
      {
        path: '/:type',
        name: "gun",
        component: Gun
      },
      {
        path: '/detail/:id',
        name: 'gunDetail',
        component: GunDetail
      },
    ]
  },

]



const router = new VueRouter({
  mode: "history",
  routes,
})



export default router
